#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    isStart=false;
    ui->hour->setText("0");
    ui->min->setText("0");
    ui->sec->setText("0");
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_start_clicked()
{
    if(isStart==false)
    {
        timer=new QTimer(this);
        //connect(timer,&QTimer::timeout,this,&Widget::update);
        QObject::connect(timer,SIGNAL(timeout()),this,SLOT(update()));
        timer->start(1000);
        isStart=true;
    }
    else
    {
        isStart=false;
        timer->stop();
        ui->hour->setText("0");
        ui->min->setText("0");
        ui->sec->setText("0");
        timer->start(1000);
        isStart=true;
    }
}

void Widget::update()
{
    QString str_sec=ui->sec->text();
    int int_sec=str_sec.toInt();
    int_sec++;
    if(int_sec==60)
    {
        ui->sec->setText("0");
        QString str_min=ui->min->text();
        int int_min=str_min.toInt();
        int_min++;
        if(int_min==60)
        {
            ui->min->setText("0");
            QString str_hour=ui->hour->text();
            int int_hour=str_hour.toInt();
            int_hour++;
            ui->hour->setText(QString::number(int_hour));
        }
        else
        {
            ui->min->setText(QString::number(int_min));
        }
    }
    else
    {
         ui->sec->setText(QString::number(int_sec));
    }

}

void Widget::on_stop_clicked()
{
    timer->stop();
}

void Widget::on_zero_clicked()
{
    isStart=false;
    timer->stop();
    ui->hour->setText("0");
    ui->min->setText("0");
    ui->sec->setText("0");

}

void Widget::on_con_clicked()
{
    timer->start();
}

void Widget::on_quit_clicked()
{
    this->close();
}


